/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         cassie
 */

package com.coocaa.launcher.pattern.normal.hotseat;

import android.content.Context;
import android.graphics.Canvas;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.coocaa.x.framework.utils.LogUtils;

public class FocusView extends ImageView {
    private int orgPosX, orgPosY, orgWidth, orgHeight;
    private int destPosX, destPosY, destWidth, destHeight;
    private int deltaX, deltaY, deltaWidth, deltaHeight;
    private int crtStep = 0;
    private int stepCount = 6;
    private boolean executeAnim = false;

    public FocusView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    // 移动到指定位置
    public void changeFocusPos(int posX, int posY, int destWidth, int destHeight) {
        executeAnim = true;
        crtStep = 0;
        this.destPosX = posX;
        this.destPosY = posY;
        this.destWidth = destWidth;
        this.destHeight = destHeight;
        LogUtils.d("focus_bg", "--------- change focus pos, will init step, X = " + posX + ", Y = " + posY + ", width = " + destWidth + ", height = " + destHeight);
        initStep();
        invalidate();
    }

    // 直接设置位置到指定位
    public void setFocusPos(int posX, int posY, int destWidth, int destHeight) {
        executeAnim = false;
        FrameLayout.LayoutParams frameParams = (FrameLayout.LayoutParams) getLayoutParams();
        frameParams.leftMargin = posX;
        frameParams.topMargin = posY;
        frameParams.width = destWidth;
        frameParams.height = destHeight;
        this.setLayoutParams(frameParams);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (!executeAnim)
            return;
        LayoutParams params = this.getLayoutParams();
        if (params instanceof FrameLayout.LayoutParams) {
            FrameLayout.LayoutParams frameParams = (FrameLayout.LayoutParams) params;
            LogUtils.d("focus_bg", "w = " + getWidth() + ", h = " + getHeight() + ", left = " + getLeft() +
                    ", top = " + getTop());
            if (getWidth() == destWidth && getHeight() == destHeight
                    && getLeft() == destPosX && getTop() == destPosY)
                return;
            // 处理宽度
            if (getWidth() != destWidth && deltaWidth != 0) {
                frameParams.width = (int) (orgWidth + calculateRatio(crtStep) * deltaWidth);//getWidth() + stepWidth;
            }
            // 处理高度
            if (getHeight() != destHeight && deltaHeight != 0) {
                frameParams.height = (int) (orgHeight + calculateRatio(crtStep) * deltaHeight);//getHeight() + stepHeight;
            }
            // 处理横坐标
            if (frameParams.leftMargin != destPosX && deltaX != 0) {
                frameParams.leftMargin = (int) (orgPosX + calculateRatio(crtStep) * deltaX);//+= stepX;
            }
            // 处理纵坐标
            if (frameParams.topMargin != destPosY && deltaY != 0) {
                frameParams.topMargin = (int) (orgPosY + calculateRatio(crtStep) * deltaY);//+= stepY;
            }
            this.setLayoutParams(frameParams);
            crtStep++;
            invalidate();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        if (oldw == 0 && w != 0) {
            LogUtils.d("focus_bg", "-----------size first init, will initStep, w = " + w + ", h = " + h);
            initStep();
        }
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void initStep() {
        orgPosX = getLeft();
        if (destPosX == orgPosX)
            deltaX = 0;
        else {
            deltaX = destPosX - orgPosX;
        }

        orgPosY = getTop();
        if (destPosY == orgPosY)
            deltaY = 0;
        else {
            deltaY = destPosY - orgPosY;
        }

        orgWidth = getWidth();
        if (destWidth == orgWidth)
            deltaWidth = 0;
        else {
            deltaWidth = destWidth - orgWidth;
        }

        orgHeight = getHeight();
        if (destHeight == orgHeight)
            deltaHeight = 0;
        else {
            deltaHeight = destHeight - orgHeight;
        }
        crtStep = 0;
        LogUtils.d("focus_bg", "************ deltaX = " + deltaX + ", deltaY = " +
                deltaY + ", deltaWdith = " + deltaWidth + ", deltaHeight = " + deltaHeight);
    }

    private float calculateRatio(int stepI) {
        stepI++;
        float ratio = 1 - (stepCount - stepI) * (stepCount - stepI) / (float) stepCount / (float) stepCount;
        return ratio;
    }
}
